package cn.yunyichina.provider.reservation.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cn.yunyichina.provider.framework.page.PageInfo;
import cn.yunyichina.provider.framework.utils.UUIDUtil;
import cn.yunyichina.provider.reservation.dao.ItemDao;
import cn.yunyichina.provider.reservation.dao.ItemRuleDao;
import cn.yunyichina.provider.reservation.entity.Item;
import cn.yunyichina.provider.reservation.service.ItemService;
import cn.yunyichina.utils.convert.JsonUtils;

/**
 * 项目
 */
@Service
public class ItemServiceImpl implements ItemService {
	
	private static final Logger logger = LoggerFactory.getLogger(ItemServiceImpl.class);

    @Resource
    private ItemDao itemDao;

    @Resource
    private ItemRuleDao itemRuleDao;

    /**
     * 后台管理：显示项目列表
     *
     * @param strJson
     * @return
     */
    public List<Item> listItem(String strJson) {
        JSONObject jsonObj = JsonUtils.parseObject(strJson);
        Item item = JsonUtils.parseObject(strJson, Item.class);

        PageInfo pageInfo = null;
        if (jsonObj.containsKey("pageInfo")) {
            pageInfo = jsonObj.getObject("pageInfo", PageInfo.class);
        } else {
            pageInfo = new PageInfo();
        }
        List<String> orderColumns = null;
        if (jsonObj.containsKey("orderColumns")) {
            orderColumns = new ArrayList<String>();
            String str = jsonObj.getString("orderColumns");
            String[] listObj = str.split(",");
            for (Object field : listObj) {
                orderColumns.add((String) field);
            }
        }

        List<String> selects = null;
        if (jsonObj.containsKey("selects")) {
            selects = new ArrayList<String>();
            String str = jsonObj.getString("selects");
            String[] listObj = str.split(",");
            for (Object field : listObj) {
                selects.add((String) field);
            }
        }

        boolean isAsc = true;
        if (jsonObj.containsKey("isAsc")) {
            isAsc = jsonObj.getBooleanValue("isAsc");
        }

        List<Item> list = itemDao.select(item, selects, orderColumns, isAsc, pageInfo);
        
        logger.debug("项目列表："+JsonUtils.toJsonString(list));
        
        return list;
    }

    /**
     * 用户显示项目列表，非后台管理
     *
     * @param strJson
     * @return
     */
    public List<Item> listItemForUser(String strJson) {
        JSONObject jsonObj = JsonUtils.parseObject(strJson);
        Item item = JsonUtils.parseObject(strJson, Item.class);

        PageInfo pageInfo = null;
        if (jsonObj.containsKey("pageInfo")) {
            pageInfo = jsonObj.getObject("pageInfo", PageInfo.class);
        } else {
            pageInfo = new PageInfo();
        }
        List<String> orderColumns = null;
        if (jsonObj.containsKey("orderColumns")) {
            orderColumns = new ArrayList<String>();
            String str = jsonObj.getString("orderColumns");
            String[] listObj = str.split(",");
            for (Object field : listObj) {
                orderColumns.add((String) field);
            }
        }

        List<String> selects = null;
        if (jsonObj.containsKey("selects")) {
            selects = new ArrayList<String>();
            String str = jsonObj.getString("selects");
            String[] listObj = str.split(",");
            for (Object field : listObj) {
                selects.add((String) field);
            }
        }

        boolean isAsc = true;
        if (jsonObj.containsKey("isAsc")) {
            isAsc = jsonObj.getBooleanValue("isAsc");
        }

        List<Item> list = itemDao.selectForUser(item, selects, orderColumns, isAsc, pageInfo);
        logger.debug("项目列表："+JsonUtils.toJsonString(list));
        return list;
    }

    public Item getItem(String strJson) {
        JSONObject jsonObj = JsonUtils.parseObject(strJson);
        return itemDao.selectById(jsonObj.getString("id"));
    }

    public Item addItem(String strJson) {
        Item item = JsonUtils.parseObject(strJson, Item.class);
        item.setId(UUIDUtil.get32UUID());
        itemDao.insert(item);

        item = itemDao.selectById(item.getId());
        return item;
    }

    public void updateItem(String strJson) {
        Item item = JsonUtils.parseObject(strJson, Item.class);
        itemDao.update(item);
    }

    public void delItem(String strJson) {
        Item item = JsonUtils.parseObject(strJson, Item.class);
        item = itemDao.selectById(item.getId());
        item.setStatus(2);
        itemDao.update(item);
    }

    /**
     * 上架，下架 stauts=1上架，status=0下架
     *
     * @param strJson
     */
    public void updateItemStatus(String strJson) {
        Item item = JsonUtils.parseObject(strJson, Item.class);
        DateFormat YYYYMMDDHHMMSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (item.getStatus() != 1) {
            item.setStatus(0);
            item.setDisableTime(YYYYMMDDHHMMSS.format(new Date()));
        } else {
            item.setEnableTime(YYYYMMDDHHMMSS.format(new Date()));
        }
        itemDao.changeItemStatus(item);
    }

}
